/**********京东头部导航栏二级标签**********/
//获取变量
var nav_top_fl = document.getElementsByClassName("nav_top_fl")[0];
var nav_top_fl_a = nav_top_fl.getElementsByTagName("a")[0];
var nav_top_fl_hidden_content = nav_top_fl.getElementsByClassName("hidden_content")[0];

//鼠标移入显示下拉框
nav_top_fl.addEventListener("mouseover", function () {
    nav_top_fl_a.className = "a_hover";
    nav_top_fl_hidden_content.style.display = "block";
});
//鼠标离开隐藏下拉框
nav_top_fl.addEventListener("mouseout", function () {
    nav_top_fl_a.removeAttribute("class");
    nav_top_fl_hidden_content.removeAttribute("style");
});






/**********京东轮播图一**********/
//获取变量
var $1_lb = document.getElementsByClassName("JD_lb_center")[0];
var $1_imgs = $1_lb.getElementsByTagName("img");
var $1_spans = $1_lb.getElementsByTagName("span");
var $1_ul = $1_lb.getElementsByTagName("ul")[0];
var $1_index = 0, $_id;
showlogo();
var $1_ul_li = $1_ul.getElementsByTagName("li");
interval();
//鼠标移入li标签跳转相应图片
for (let i = 0, leg = $1_ul_li.length; i < leg; i++) {
    $1_ul_li[i].addEventListener("mouseover", function () {
        hidden();
        $1_index = i;
        display();
    })
}
//点击鼠标切换图片
for (let i = 0, leg = $1_spans.length; i < leg; i++) {
    $1_spans[i].addEventListener("click", function () {
        hidden();
        if (i == 0) {  //点击的左边切换键
            if ($1_index == 0) {
                $1_index = $1_imgs.length - 1;
            } else {
                $1_index--;
            }
        } else {   //点击的右边切换键
            if ($1_index == $1_imgs.length - 1) {
                $1_index = 0;
            } else {
                $1_index++;
            }
        }
        display();
    });
}
//鼠标移入停止轮播事件
$1_lb.addEventListener("mouseover", function () {
    clearInterval($_id);
})

$1_lb.addEventListener("mouseout", function () {
    interval();
})

//轮播图函数
function interval() {
    $_id = setInterval(function () {
        hidden();
        $1_index++;
        if ($1_index == $1_imgs.length) {
            $1_index = 0;
        }
        display();
    }, 1000);
}
//给ul动态添加li标签
function showlogo() {
    let $1_ul = $1_lb.getElementsByTagName("ul")[0];
    let html = "";
    for (let i = 0; i < $1_imgs.length; i++) {
        if (i == $1_index) {
            html = html + "<li class =\"img_li_color\"></li>";
        } else {
            html = html + "<li></li>";
        }
    }
    $1_ul.innerHTML = html;
}
//给li标签动态添加属性
function hidden() {
    $1_ul_li[$1_index].removeAttribute("class");
    $1_imgs[$1_index].removeAttribute("class");
}
function display() {
    $1_ul_li[$1_index].className = "img_li_color";
    $1_imgs[$1_index].className = "on_img";
}






/**********京东秒杀倒计时**********/
var str = "2019-11-28 10:10:30" //设置商品活动结束时间
startInt(str);
function startInt(str) {
    var id = setInterval(function () {
        var hour = document.getElementById("hour");
        var minute = document.getElementById("minute");
        var second = document.getElementById("second");


        let setDate = new Date(str);  //结束时间
        var date = new Date();        //获取当前日期

        let res = setDate.getTime() - date.getTime();
        //1小时=60分钟  1分钟=60秒  1=1000毫秒

        let lingshi = res / 1000 / 60 / 60;
        var a = Math.floor(lingshi);
        var b = Math.floor((lingshi - a) * 60);
        var c = Math.floor(((lingshi - a) * 60 - b) * 60);

        if (toDecimal2(a, 1) < 0) {
            clearInterval(id)
            return;
        }
        hour.innerText = toDecimal2(a, 2)
        minute.innerText = toDecimal2(b, 2)
        second.innerText = toDecimal2(c, 2)
    }, 1000)
}
/*不满足两位数 用0补充*/
function toDecimal2(x, num) {
    x = x + "";
    for (let a = x.length; a < num; a++) {
        x = "0" + x;
    }
    return x;
}

/*************京东头部导航地区点击改变样式************/
var hidden_content = document.getElementsByClassName("hidden_content")[0];
var hidden_content_a = hidden_content.getElementsByTagName("ul")[0].getElementsByTagName("a");
var hidden_content_zhuyao = document.getElementsByClassName("zhuyao")[0];

for(let i=0, leg=hidden_content_a.length; i<leg; i++){
    hidden_content_a[i].addEventListener("mouseover",function(){
        if(this.className!="change_aHover1"){
            this.className="change_aHover2";
        }
    })
    hidden_content_a[i].addEventListener("mouseout",function(){
        if(this.className!="change_aHover1"){
            this.removeAttribute("class");
        }
    })
    hidden_content_a[i].addEventListener("click",function(){
        for(let j=0;j<hidden_content_a.length; j++){
            hidden_content_a[j].removeAttribute("class");
        }
        this.className="change_aHover1";
        hidden_content_zhuyao.textContent=this.textContent;
    })
}
